package cn.tedu.store.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tedu.store.bean.Cart;
import cn.tedu.store.bean.CartVo;
import cn.tedu.store.bean.ResponseResult;
import cn.tedu.store.mapper.CartMapper;
import cn.tedu.store.service.ICartService;

@Controller
@RequestMapping("/cart")
public class CartController extends BaseController{
	
	@Resource
	private ICartService cartService;
	
	/**
	 * 显示购物车cart.jsp的页面
	 * @return
	 */
	@RequestMapping("/showCart.do")
	public String showCart(HttpSession session,Map<String,Object> map){
		List<CartVo> list=cartService.getAll(this.getUid(session));
		map.put("listCartVo", list);
		return "cart";
	}
	
	
	@RequestMapping("deleteById.do")
	public String deleteById(Integer id){
		cartService.deleteByCartId(id);
		return "redirect:../cart/showCart.do";
	}
	
	@RequestMapping("/deleteByBatch.do")
	public String deleteByBatch(Integer[] ids){
		cartService.deleteByBatch(ids);
		return "redirect:../cart/showCart.do";
	}
	
	/**
	 * 添加购物车里面的商品
	 * @param goodsid
	 * @param session
	 * @param count
	 * @return
	 */
	@RequestMapping("/addCart.do")
	@ResponseBody
	public ResponseResult<Void> addCart(String goodsid,HttpSession session,Integer count){
		Cart cart=new Cart();
		System.out.println("goodsid:"+goodsid);
		cart.setGoodsid(goodsid);
		cart.setUserid(this.getUid(session));
		cart.setCount(count);
		cartService.addCart(cart);
		ResponseResult<Void> rr=new ResponseResult<Void>(1,"添加成功");
		return rr;
		
	}
	
	//通过id修改商品数量
	@RequestMapping("/updateCountById.do")
	@ResponseBody
	public ResponseResult<Void> updateCountById(Integer id,Integer count){
		cartService.updateById(id, count);
		ResponseResult<Void> rr=new ResponseResult<Void>(1,"修改成功");
		return rr;
	}
//	@RequestMapping("/addCount.do")
//	public String addCount(Integer num,HttpSession session,Integer cartid){
//		cartService.addCount(this.getUid(session),num,cartid);
//		return "redirect:../cart/showCart.do";
//	}
//	@RequestMapping("/subtractCount.do")
//	public String subtractCount(Integer num,HttpSession session,Integer cartid){
//		cartService.subtractCount(this.getUid(session), num, cartid);
//		return "redirect:../cart/showCart.do";
//	}
	
}
